﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Diagnostics;

namespace SELF.Controls
{
    public partial class frm_CourseHistory : DevExpress.XtraEditors.XtraForm
    {
        public frm_CourseHistory()
        {
            InitializeComponent();
        }

        private string ticketID;
        private TreeNode tnRoot = new TreeNode();
        DataRow drCurrent;

        public string TicketID
        {
            get { return ticketID; }
            set { ticketID = value; }
        }

        lcmpt.Connection Conn = new lcmpt.Connection();
        lcmpt.MouseHelper mos = new lcmpt.MouseHelper();
        private void frm_CourseHistory_Load(object sender, EventArgs e)
        {
            Image image_bg = Image.FromFile(cGobal.ImagePath.ToString() + cGobal.ThemeID.ToString() + @"\CourseHistory\main_history.png");
            this.BackgroundImage = image_bg;
            this.BackgroundImageLayout = ImageLayout.Tile;

            new TouchScrollHelper(gridView1);

            DrawNode();


        }

        private DataSet GetHistory(string ticket,string lectureName)
        {
            try
            {
                DataSet ds = new DataSet();
                string sqlHistory = "EXECUTE [dbo].[sp_Get_History] '@TicketID','@LectureName'";
                sqlHistory = sqlHistory.Replace("@TicketID", TicketID);
                sqlHistory = sqlHistory.Replace("@LectureName", lectureName);
                ds = Conn.Retreive(sqlHistory);
                return ds;
            }
            catch
            {
                DataSet ds = new DataSet();
                return ds;
            }
        }
        private void DrawNode()
        {
            string sql = @"SELECT DISTINCT CASE (SELECT FL.LearningType FROM [dbo].[Fact_LearningTicket] FL WHERE FL.TicketID = @TicketID)
	                           WHEN 'Self' THEN (SELECT TypeCourse FROM dbo.Dim_SetCourse WHERE SetCourseID = (SELECT LT.SetCourseID FROM Fact_LearningTicket LT WHERE LT.TicketID = His.TicketID))
	                           WHEN 'Live' THEN (SELECT TypeCourse FROM dbo.Dim_SetCourse_Sud WHERE SetCourseID_Sud = (SELECT LT.SetCourseID FROM Fact_LearningTicket LT WHERE LT.TicketID = His.TicketID))
	                           END AS TypeCourse
                        FROM Dim_TicketHistory His
                        WHERE His.TicketID = @TicketID";
            sql = sql.Replace("@TicketID", TicketID);
            DataSet dsRoot = Conn.Retreive(sql);
            treeView1.Nodes.Clear();
            tnRoot.Text = "คอร์สที่เคยเรียน";
            tnRoot.Name = "rootNode";
            tnRoot.Expand();
            treeView1.Nodes.Add(tnRoot);
            foreach (DataRow dr in dsRoot.Tables[0].Rows)
            {
                TreeNode tnParent = new TreeNode();
                tnParent.Text = "ประเภทคอร์ส - "+dr[0].ToString().Trim();
                tnParent.Name = dr[0].ToString().Trim();
                tnParent.Expand();
                tnRoot.Nodes.Add(tnParent);
                DrawChildNode(tnParent, tnParent.Name);
            }
            treeView1.ExpandAll();

        }

        private void DrawChildNode(TreeNode parent, string ParentId)
        {
            string sql = @"SELECT b.CourseName
                      FROM [dbo].[Fact_LearningTicket] as a,
                      [dbo].[Dim_Courses] as b
                      where TicketID = '@TicketID'
                      and a.CourseID = b.CourseID";
            sql = sql.Replace("@TicketID", TicketID);
            DataSet dsChild = Conn.Retreive(sql);
            foreach (DataRow dr in dsChild.Tables[0].Rows)
            {
                TreeNode child = new TreeNode();
                child.Text = "คอร์ส - "+ dr[0].ToString().Trim();
                //child.Name = dr[0].ToString().Trim();
                child.Name = "Course";
                child.Collapse();
                parent.Nodes.Add(child);
                DrawChildNode2(child, child.Name);
            }
        }

        private void DrawChildNode2(TreeNode parent, string ParentId)
        {
            string sql = @"SELECT DISTINCT His.[LectureName]
                          FROM [dbo].[Dim_TicketHistory] His
                          WHERE His.TicketID = '@TicketID'";
            sql = sql.Replace("@TicketID", TicketID);
            DataSet dsChild = Conn.Retreive(sql);
            foreach (DataRow dr in dsChild.Tables[0].Rows)
            {
                TreeNode child = new TreeNode();
                child.Text = dr[0].ToString().Trim();
                child.Name = dr[0].ToString().Trim();
                child.Collapse();
                parent.Nodes.Add(child);
            }
        }

        private void label4_Click(object sender, EventArgs e)
        {
            Dialog_Close cls = new Dialog_Close();
            cls.ShowDialog();
            if (cls.DialogResult == DialogResult.Yes)
            {
                this.DialogResult = DialogResult.Ignore;
                this.Close();
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Parent != null)
            {
                string pNode = e.Node.Parent.Name;
                if (pNode == "Course")
                {
                    string nSelf = e.Node.Text;
                    DataSet ds = GetHistory(TicketID, nSelf);
                    //fill datagrid
                    FillGrid(ds);
                }
                else
                {
                    DataSet ds = GetHistory(TicketID, "");
                    //fill datagrid
                    FillGrid(ds);
                }
            }
            else
            {
                DataSet ds = GetHistory(TicketID, "");
                //fill datagrid
                FillGrid(ds);
            }
        }

        private void FillGrid(DataSet dsData)
        {
            CoreEngine.Util.DevExpressDataGridUtil.Fill(gridControl1, dsData);
            CoreEngine.Util.DevExpressDataGridUtil _config = new CoreEngine.Util.DevExpressDataGridUtil();
            _config.SetParameterColumn("TypeCourse", "ประเภทคอร์ส", true, 100, DevExpress.Utils.HorzAlignment.Center);
            _config.SetParameterColumn("CourseName", "ชื่อคอร์ส", true, 100, DevExpress.Utils.HorzAlignment.Center);
            _config.SetParameterColumn("LectureName", "ครั้งที่", true, 100, DevExpress.Utils.HorzAlignment.Center);
            _config.SetParameterColumn("Description", "รายละเอียด", true, 100, DevExpress.Utils.HorzAlignment.Center);
            _config.SetParameterColumn("BeginTime", "เข้าเรียน", true, 100, DevExpress.Utils.HorzAlignment.Center);
            _config.SetParameterColumn("EndTime", "เลิกเรียน", true, 100, DevExpress.Utils.HorzAlignment.Center);
            _config.SetParameterColumn("Usage", "รวมเรียน", true, 100, DevExpress.Utils.HorzAlignment.Center);
            _config.SetParameterColumnFormat("BeginTime", CoreEngine.Util.DevExpressDataGridUtil.FormatType.DateTime, "hh:MM:ss");
            _config.SetParameterColumnFormat("EndTime", CoreEngine.Util.DevExpressDataGridUtil.FormatType.DateTime, "hh:MM:ss");
            _config.GridViewRanderConfig(gridView1);
            _config.GridViewRanderDataFormat(gridControl1);
            CoreEngine.Util.DevExpressDataGridUtil.BestFitAllColumn(gridView1);
        }

        private void panel1_Click(object sender, EventArgs e)
        {
            HisSumary hls = new HisSumary();
            hls.TicketID = TicketID;
            hls.ShowDialog();
        }

        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            drCurrent = gridView1.GetDataRow(e.RowHandle);
            setLabelValue(drCurrent);
        }
        private void setLabelValue(DataRow row)
        {
            lbl1.Text = row["TypeCourse"].ToString();
            lbl2.Text = row["CourseName"].ToString();
            lbl3.Text = row["LectureName"].ToString();
            lbl4.Text = row["Description"].ToString();
            lbl5.Text = row["BeginTime"].ToString();
            lbl6.Text = row["EndTime"].ToString();
            lbl7.Text = row["Usage"].ToString();


        }
        Process proc = new Process();
        private void label3_Click(object sender, EventArgs e)
        {
            try
            {
                Process[] prs = Process.GetProcesses();
                foreach (Process pr in prs)
                {
                    Console.WriteLine(pr.ProcessName);
                    if (pr.ProcessName == "chrome")
                    {
                        pr.Kill();
                    }
                }
                this.TopMost = false;
                proc.StartInfo.FileName = Config.Configurations.ChromePath;
                string name = " --kiosk " + Config.Configurations.BookingURL;
                proc.StartInfo.Arguments = name;
                proc.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
                proc.Start();
                proc.WaitForExit();
            }
            catch { }
            //this.Close();
        }

        private void label3_MouseHover(object sender, EventArgs e)
        {
            if (!Conn.IsOnline)
            {
                mos.DisableMouseClicks();
            }
        }

        private void label3_MouseLeave(object sender, EventArgs e)
        {
            if (!Conn.IsOnline)
            {
                mos.EnableMouseClicks();
            }
        }

    }
}